/*
 * Copyright 2007-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.paoding.rose.impl.util;

import net.paoding.rose.Rc;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;

/**
 * 
 * @author qieqie.wang@paoding.net
 * 
 */
public class SpringUtils {
	private static Log logger = LogFactory.getLog(SpringUtils.class);

	public static Object getBean(ListableBeanFactory bf, Class<?> beanClass) {
		String[] names = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
				bf, beanClass);
		return names.length == 0 ? null : bf.getBean(names[0]);
	}

	public static String[] getBeanNames(ListableBeanFactory bf,
			Class<?> beanClass) {
		return BeanFactoryUtils.beanNamesForTypeIncludingAncestors(bf,
				beanClass);
	}

	public static Object getBean(ListableBeanFactory bf, String beanName) {
		return bf.containsBean(beanName) ? bf.getBean(beanName) : null;
	}

	/**
	 * 
	 * 只autowire applicationContext的
	 * 
	 * @param clazz
	 * @return
	 */
	public static Object createBean(Class<?> clazz) {
		return createBean(clazz, Rc.getRootWebApplicationContext());
	}

	public static Object createBean(Class<?> clazz, String moduleName) {
		return createBean(clazz, Rc.getWebApplicationContext(moduleName));
	}

	/**
	 * 
	 * @param clazz
	 * @param context
	 * @return
	 */
	public static Object createBean(Class<?> clazz, ApplicationContext context) {
		Object bean = context.getAutowireCapableBeanFactory().createBean(clazz,
				AutowireCapableBeanFactory.AUTOWIRE_NO, false);
		if (logger.isDebugEnabled()) {
			logger.debug("create spring bean: " + bean.getClass() + "@"
					+ bean.hashCode());
		}
		return bean;
	}

	public static Object autowire(Object bean) {
		return autowire(bean, Rc.getRootWebApplicationContext());
	}

	public static Object autowire(Object bean, String moduleName) {
		return autowire(bean, Rc.getWebApplicationContext(moduleName));
	}

	/**
	 * 
	 * @param bean
	 * @param context
	 */
	public static Object autowire(Object bean, ApplicationContext context) {
		context.getAutowireCapableBeanFactory().autowireBeanProperties(bean,
				AutowireCapableBeanFactory.AUTOWIRE_NO, false);
		return context.getAutowireCapableBeanFactory().initializeBean(bean,
				bean.getClass().getName());
	}

}
